package com.sl.au.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Grading;
import com.sl.au.entity.Part;
import com.sl.au.repository.GradingRepository;
import com.sl.au.service.GradingService;

@Service("GradingService")
@Transactional
public class GradingServiceImpl extends BaseServiceImpl implements GradingService{
	
	@Autowired
	GradingRepository gradingRepository;
	
	@Override
	public GradingRepository getRepository() {
		return this.gradingRepository;
	}
	
	@Override
	public List<Map<String, Object>> getNodeMap(List<Grading> node) {
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		for (Grading p : node) {
			if (null != p) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("parentId", p.getGradingParent());
				
//				if (p.getGroupParent().equals("0")) {
//					String Id = p.getId();
//					if (StringUtils.hasText(Id)) {
//						map.put("name", p.getGroupName());
//					}
//				} else {
//					map.put("name", String.valueOf(p.getGroupOrder())+p.getGroupName());
//				}
				map.put("name", String.valueOf((p.getGradingOrder())/2)+p.getGradingName());
				if (StringUtils.hasText(p.getGradingParent())) {
					map.put("isParent", p.getGradingParent());
				}
				map.put("level", p.getGradingLevel().getExtended1());
				map.put("open", true);
				nodeMap.add(map);
			}
		}
		return nodeMap;
	}


	@Override
	public List<Grading> getAllNodes() {
		List<Grading> nodes = new ArrayList<Grading>();
		nodes = gradingRepository.findAllNodes();
		return nodes;
	}

	/*********** 获取未绑定节点 *************/
	@Override
	public List<Grading> getGradingByKey(int start, int length, String key, String orderName, String dir) {
		Page<Grading> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(key != null && org.springframework.util.StringUtils.hasText(key)) {
			pages = gradingRepository.getGradingByKey(key, pageable);
		}else {
			pages = gradingRepository.getGrading(pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getGradingByKeyCount(String key) {
		long count = 0;
		if(key != null && org.springframework.util.StringUtils.hasText(key)) {
			count = gradingRepository.getGradingByKeyCount(key);
		}else {
			count = gradingRepository.getGradingCount();
		}
		return count;
	}
	
	/************ 获取子节点 ************/
	@Override
	public List<Grading> getChildrenGradingByKey(int start, int length, String id, String key, String orderName, String dir) {
		Page<Grading> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = gradingRepository.findByGradingParentAndKey(id, key, pageable);
		}else {
			pages = gradingRepository.findByGradingParent(id,pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getChildrenGradingByKeyCount(String id, String key) {
		int count = 0;
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)) {
			count = gradingRepository.findByGradingParentAndKey(id, key, pageable).getContent().size();
		}else {
			count =  gradingRepository.findByGradingParent(id,pageable).getContent().size();
		}
		return count;
	}
	
	/**********************************/
	@Override
	public void deleteNodes(String[] ids) {
		if (ids.length > 0) {
			for (String id : ids) {
				if (StringUtils.hasText(id)) {
					gradingRepository.setGrading(id);
				}
			}
		}
	}

	@Override
	public void setGradingOrder(String groupParent) {
		List<Grading> gradings = gradingRepository.getGradingsByParent(groupParent);
		if(gradings != null) {
			Grading grading = null;
			for(int i=0;i<gradings.size();i++) {
				grading = gradings.get(i);
				grading.setGradingOrder(i*2);
				gradingRepository.save(grading);
			}
		}
	}

	@Override
	public String getGradingIdentify(String id) {
		Grading grading = gradingRepository.findOne(id);
		String identify = grading.getGradingIdentify();
		String parent = grading.getGradingParent();
		if(!"0".equals(parent)) {
			identify = this.getGradingIdentify(parent) + identify;
		}
		return identify;
	}
	

}
